package com.songzhili.nohttpdemo.api;

import com.alibaba.fastjson.JSON;
import com.yanzhenjie.nohttp.Headers;
import com.yanzhenjie.nohttp.Logger;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.RestRequest;
import com.yanzhenjie.nohttp.rest.StringRequest;

/**
 * Created by Administrator on 2018/1/4 0004.
 */

public class JavaBeanRequest<T>extends RestRequest<T> {
    // 要解析的JavaBean的class。
    private Class<T> clazz;

    public JavaBeanRequest(String url, Class<T> clazz) {
        this(url,RequestMethod.GET,clazz);
    }

    public JavaBeanRequest(String url, RequestMethod requestMethod, Class<T> clazz) {
        super(url, requestMethod);
        this.clazz = clazz;
    }

    @Override
    public T parseResponse(Headers responseHeaders, byte[] responseBody) throws Exception {
        String responseString = StringRequest.parseResponseString(responseHeaders, responseBody);
        Logger.e(responseString);
        return JSON.parseObject(responseString,clazz);
    }
}
